package com.test.one.springboottestpne.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.one.springboottestpne.entity.User;
import com.test.one.springboottestpne.service.UserService;
import com.test.one.springboottestpne.utils.MD5Utils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    UserService userService;


    @Autowired
    StringRedisTemplate stringRedisTemplate;


    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public Map<String,String> login(@Valid  User user, HttpServletResponse response){
        User user1 = new User();
        user1.setLoginName(user.getLoginName());
        List<User> list = userService.selectByCondition(user1);
        Map<String,String> result = new HashMap<>();
        if (CollectionUtils.isEmpty(list)){
            result.put("code","10001");
            result.put("msg","用户名不存在");
            return result;
        }

        User user2 = list.get(0);
        String password = user2.getPassword();
        String pwd = MD5Utils.getPwd(user.getPassword() + user2.getSalt());
        if (!password.equals(pwd)){
            result.put("code","10002");
            result.put("msg","密码错误");
            return result;
        }
        result.put("code","10000");
        result.put("msg","登录成功");
        String id = UUID.randomUUID().toString();
        Cookie cookie = new Cookie("user",id);
        cookie.setDomain("localhost");
        cookie.setPath("/");
        response.addCookie(cookie);
        stringRedisTemplate.expire(id,1, TimeUnit.HOURS);
        stringRedisTemplate.opsForValue().set(id,JSON.toJSONString(user2));
        return result;
    }


    @RequestMapping(method=RequestMethod.GET)
    public Map<String,Object> getUserList(User user){
        logger.info("每页多少条数据："+user.getPageNum());
        logger.info("当前页:"+ user.getPageSize());
        PageHelper.startPage(user.getPageNum(), user.getPageSize());
        List<User> list = userService.selectByCondition(new User());
        PageInfo<User> page = new PageInfo<>(list);
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("result",list);
        return map;
    }

    @RequestMapping(value = "/find/{id}/{password}",method=RequestMethod.GET)
    @ApiOperation("根据用户id查询用户信息")
    public User getUserByID(@ApiParam("用户id") @PathVariable String id,@PathVariable String password){
        System.out.println("--------------"+id);
        System.out.println("--------------"+password);
        User user = new User();
        user.setLoginName("admin");
        user.setPassword("123");
        return user;
    }


    @RequestMapping(method = RequestMethod.POST)
    public User addUser( User user){
        return new User();
    }


    @RequestMapping(method = RequestMethod.PUT)
    public User updateUser(@RequestBody  User user){
        System.out.println(">>>>>>>>>>>"+JSON.toJSONString(user));
        return new User();
    }
}
